package edu.isistan.matching.structures.elements;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class OperationElement extends WSDLElement {

	MessageElement inMessage = null;

	MessageElement outMessage = null;

	/**
	 * Según <b>http://www.w3.org/TR/wsdl </b>, el mensaje <b>Fault</b>
	 * corresponde a: <i>The optional fault elements specify the abstract
	 * message format for any error messages that may be output as the result of
	 * the operation (beyond those specific to the protocol).</i>
	 * 
	 */
	List<MessageElement> faultMessages = new ArrayList<MessageElement>();

	public OperationElement(String nombre) {
		super(nombre);
	}

	public MessageElement getInMessage() {
		return inMessage;
	}

	public void setInMessage(MessageElement inMessage) {
		this.inMessage = inMessage;
		if (this.inMessage != null) {
			this.inMessage.setParentElement(this);
		}
	}

	public MessageElement getOutMessage() {
		return outMessage;
	}

	public void setOutMessage(MessageElement outMessage) {
		this.outMessage = outMessage;
		if (this.outMessage != null) {
			this.outMessage.setParentElement(this);
		}
	}

	public List<MessageElement> getFaultMessages() {
		return faultMessages;
	}

	public void addFaultMessage(MessageElement faultMessage) {
		this.faultMessages.add(faultMessage);
	}

	public void setFaultMessages(List<MessageElement> faultMessages) {
		this.faultMessages = faultMessages;
		for (Iterator<MessageElement> iterator = faultMessages.iterator(); iterator.hasNext();) {
			MessageElement mje = iterator.next();
			if (mje != null) {
				mje.setParentElement(this);
			}
		}
	}

	public String toString() {
		return "  operation: " + getName() + " , \\\n Message In:" + getInMessage() + ", \\\n Message Out:" + getOutMessage();
	}

	public PortTypeElement getPortType() {
		if (getParentElement() != null && (getParentElement() instanceof PortTypeElement)) {
			return (PortTypeElement) getParentElement();
		}
		return null;
	}

	public void setPortType(PortTypeElement element) {
		setParentElement(element);
	}

}
